{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.policinginstitute.org/publication/the-minneapolis-domestic-violence-experiment/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "from typing import Dict, List\n",
    "\n",
    "from dowhy import CausalModel\n",
    "# statsmodels' instrument variable implementation\n",
    "from statsmodels.sandbox.regression.gmm import IV2SLS\n",
    "\n",
    "# Display only double digits\n",
    "pd.set_option('display.float_format', '{:.2f}'.format)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data Cleaning "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "TREATMENT_CODE_NAME_MAPPING: Dict[int, str] = {\n",
    "    1: 'Arrest',\n",
    "    2: 'Advise',\n",
    "    3: 'Separate',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>T_RANDOM</th>\n",
       "      <th>MONTH</th>\n",
       "      <th>YEAR</th>\n",
       "      <th>CLOCK</th>\n",
       "      <th>TIME</th>\n",
       "      <th>P_REPORT</th>\n",
       "      <th>CCN</th>\n",
       "      <th>V_RACE</th>\n",
       "      <th>S_RACE</th>\n",
       "      <th>...</th>\n",
       "      <th>P_ASSLT2</th>\n",
       "      <th>P_CALMED</th>\n",
       "      <th>P_REFER</th>\n",
       "      <th>P_ADVISE</th>\n",
       "      <th>T_FINAL</th>\n",
       "      <th>REASON1</th>\n",
       "      <th>REASON2</th>\n",
       "      <th>REASON3</th>\n",
       "      <th>REASON4</th>\n",
       "      <th>R_RELATE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>372020123</td>\n",
       "      <td>3</td>\n",
       "      <td>5.00</td>\n",
       "      <td>81.00</td>\n",
       "      <td>125.00</td>\n",
       "      <td>35.00</td>\n",
       "      <td>1</td>\n",
       "      <td>83905.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>441050111</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>81.00</td>\n",
       "      <td>515.00</td>\n",
       "      <td>40.00</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>...</td>\n",
       "      <td>2.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>281200111</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>81.00</td>\n",
       "      <td>1910.00</td>\n",
       "      <td>15.00</td>\n",
       "      <td>1</td>\n",
       "      <td>77252.00</td>\n",
       "      <td>3</td>\n",
       "      <td>3.00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>681260123</td>\n",
       "      <td>3</td>\n",
       "      <td>7.00</td>\n",
       "      <td>81.00</td>\n",
       "      <td>210.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>3.00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>3</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>232010123</td>\n",
       "      <td>3</td>\n",
       "      <td>5.00</td>\n",
       "      <td>81.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>74836.00</td>\n",
       "      <td>1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>...</td>\n",
       "      <td>1.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>9.00</td>\n",
       "      <td>3</td>\n",
       "      <td>9.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          ID  T_RANDOM  MONTH  YEAR   CLOCK  TIME  P_REPORT      CCN  V_RACE  \\\n",
       "0  372020123         3   5.00 81.00  125.00 35.00         1 83905.00       1   \n",
       "1  441050111         1   5.00 81.00  515.00 40.00         1      NaN       1   \n",
       "2  281200111         1   5.00 81.00 1910.00 15.00         1 77252.00       3   \n",
       "3  681260123         3   7.00 81.00  210.00   NaN         0      NaN       3   \n",
       "4  232010123         3   5.00 81.00  100.00   NaN         1 74836.00       1   \n",
       "\n",
       "   S_RACE  ...  P_ASSLT2  P_CALMED  P_REFER  P_ADVISE  T_FINAL  REASON1  \\\n",
       "0    1.00  ...      1.00      9.00     9.00      9.00        3     1.00   \n",
       "1    2.00  ...      2.00      9.00     9.00      9.00        1     1.00   \n",
       "2    3.00  ...      1.00      9.00     9.00      9.00        1     1.00   \n",
       "3    3.00  ...      1.00      9.00     9.00      9.00        3     1.00   \n",
       "4    1.00  ...      1.00      9.00     9.00      9.00        3     9.00   \n",
       "\n",
       "   REASON2  REASON3  REASON4  R_RELATE  \n",
       "0     1.00     1.00     1.00       NaN  \n",
       "1     1.00     1.00     1.00       NaN  \n",
       "2     1.00     1.00     1.00       NaN  \n",
       "3     1.00     1.00     1.00       NaN  \n",
       "4     1.00     1.00     1.00       NaN  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_stata(\n",
    "    \"../data/mdve.dta\"\n",
    ")\n",
    "\n",
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# drop if the actual treatment is \"other\"\n",
    "data = data[data['T_FINAL'] != 4]\n",
    "\n",
    "# coddle\n",
    "data['z_coddle'] = data['T_RANDOM'].apply(lambda x: 0 if x == 1 else 1)\n",
    "data['d_coddle'] = data['T_FINAL'].apply(lambda x: 0 if x == 1 else 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGAAAAAUCAYAAAByKzjvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAABJ0AAASdAHeZh94AAAFWUlEQVR4nO3ZfejfVRUH8NdvLtE0tSY1lCKXTqSkuR60cJalixTDaVHE1AwliDVmjVJpHo9hm9S0iVG5xaYmPtDUqIViJOrswQqHxbSUdKX5OMUnttS1/rj3M7+/zz6/x6/uB+Ubvtzf59xz7j2fc+55+NzfwNatW72OicOkiVbg/x2Tu4iZeRk+if0i4oUdq9L/HjLzffgjTo+IFb1zA+0UlJkfwO+xMCIu7KFfgPdjOvbGJmzADbgkIjZ2bPxpfAQz8F68CVdGxNwxKD8XV9TH7V6gX2TmFMzBsTgY++JF/BkrsTIi/tMhN4DT6u/dGMA9WIFL2zKZeT0OwwER8XxD70pB5+NZ/KBFPwO74WYsw5V4Gefi7sx8e8da38Q8xQEPd5tgaNQ1L8HzI/H2gc9gOQ5VDt73sBrvUYx5bTV2Gz/BpXgnrqq8b1TstqqDfzGmYn4vcVAKyszpOAorImJTa4E9ImJze9XMPB9n4yx8uTV9Bh7C/Uok3NKhWCfqS6/ERlyHhaOVHSP+hk9hTe+pzcyzcSdOxAmKU5q5Ofg8HsAHI+LJSt+58p2UmTdExHWNTETcmZn34kuZuaTZqx0BX1RC6Zq2ll3Gr7i2jgd0yNwSEfdFxHharfn4GE7Fa1aHIuLXEfHzdsqIiEfxw/r40ZbYnDoubYxfZV7Eovo4r2O7q/EOHN0Q2g44ClvwuzG8w3F1vHsMMsMiMw/CEiyLiNterXXHgZfq+HKLPrWOf++QaWizakT04o46bnPAthSUmbspufqe4TqfzFyI3bGnUpQPV4y/ZC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right)$"
      ],
      "text/plain": [
       "(314, 29)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['ID', 'T_RANDOM', 'MONTH', 'YEAR', 'CLOCK', 'TIME', 'P_REPORT', 'CCN',\n",
       "       'V_RACE', 'S_RACE', 'V_CHEM', 'S_CHEM', 'S_DMNOR1', 'S_DMNOR2',\n",
       "       'WEAPON', 'GUNS', 'P_ASSLT1', 'P_ASSLT2', 'P_CALMED', 'P_REFER',\n",
       "       'P_ADVISE', 'T_FINAL', 'REASON1', 'REASON2', 'REASON3', 'REASON4',\n",
       "       'R_RELATE', 'z_coddle', 'd_coddle'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate Assigned and Delivered Treatments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The table 3.3 in Chapter 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>T_FINAL</th>\n",
       "      <th>Arrest</th>\n",
       "      <th>Advise</th>\n",
       "      <th>Separate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>T_RANDOM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Arrest</th>\n",
       "      <td>98.91</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Advise</th>\n",
       "      <td>17.59</td>\n",
       "      <td>77.78</td>\n",
       "      <td>4.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Separate</th>\n",
       "      <td>22.81</td>\n",
       "      <td>4.39</td>\n",
       "      <td>72.81</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "T_FINAL   Arrest  Advise  Separate\n",
       "T_RANDOM                          \n",
       "Arrest     98.91    0.00      1.09\n",
       "Advise     17.59   77.78      4.63\n",
       "Separate   22.81    4.39     72.81"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compliance_data = (\n",
    "    data\n",
    "    .groupby(by=['T_RANDOM', 'T_FINAL'])\n",
    "    .agg({'ID': 'count'})\n",
    "    .reset_index()\n",
    "    .pivot_table(\n",
    "        values='ID',\n",
    "        index='T_RANDOM',\n",
    "        columns='T_FINAL'\n",
    "    )\n",
    "    .fillna(0)\n",
    "    .astype(int)\n",
    "    .rename(\n",
    "        index=TREATMENT_CODE_NAME_MAPPING,\n",
    "        columns=TREATMENT_CODE_NAME_MAPPING\n",
    "    )\n",
    ")\n",
    "\n",
    "compliance_data = (\n",
    "    compliance_data\n",
    "    .div(compliance_data.sum(axis=1), axis=0)\n",
    "    * 100\n",
    ")\n",
    "\n",
    "compliance_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Causal Inference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `DoWhy` Implementation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `statsmodels` Implementation"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mastering-py-metrics",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
